package com.xuerhua.yxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.dao.AttachmentListMapper;
import com.xuerhua.yxt.dao.CoordinationOfficeMapper;
import com.xuerhua.yxt.dao.CoordinationOfficeSendeeMapper;
import com.xuerhua.yxt.dao.SchoolWechatTemplateMapper;
import com.xuerhua.yxt.model.AttachmentList;
import  com.xuerhua.yxt.model.CoordinationOffice;
import com.xuerhua.yxt.model.CoordinationOfficeSendee;
import com.xuerhua.yxt.model.SchoolInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.OfficeQueryVO;
import com.xuerhua.yxt.reqVo.OfficeReqVO;
import com.xuerhua.yxt.service.ICoordinationOfficeService;
import com.xuerhua.yxt.service.ISchoolInfoService;
import com.xuerhua.yxt.view.CoordinationOfficeView;
import com.xuerhua.yxt.wechat.utils.TemplateUtil;

@Service("coordinationOfficeService")
public class CoordinationOfficeServiceImpl implements ICoordinationOfficeService {

	@Autowired
	private CoordinationOfficeMapper coordinationOfficeMapper;
	@Autowired
	private CoordinationOfficeSendeeMapper CoordinationOfficeSendeeMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper;
	@Autowired
	private ISchoolInfoService schoolInfoService;
	@Autowired
	private SchoolWechatTemplateMapper schoolWechatTemplateMapper;
	
	@Override
	public int insertSelective(CoordinationOffice record) {
		return coordinationOfficeMapper.insertSelective(record);
	}

	@Override
	public CoordinationOffice selectByPrimaryKey(Integer id) {
		return coordinationOfficeMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(CoordinationOffice record) {
		return coordinationOfficeMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<CoordinationOffice> selectListByPage(Page<CoordinationOffice> page) {
		return coordinationOfficeMapper.selectListByPage(page);
	}

	@Override
	public int createOffice(CoordinationOffice record, OfficeReqVO reqVO) {
		coordinationOfficeMapper.insertSelective(record);
		AttachmentList attachment = new AttachmentList();
		attachment.setTeacherId(record.getSenderId());
		attachment.setRelationType(RelationType.COORDINATION_OFFICE.getCode());
		attachment.setRelationId(Long.valueOf(record.getId()));
		attachment.setSchoolId(record.getSchoolId());
		attachmentListMapper.updateRelationId(attachment);
		new Thread(new CoordinationOfficeNoticeExecutor(reqVO, record)).start();
		return 1;
	}
	
	@Override
	public CoordinationOfficeView selectViewDetail(OfficeQueryVO queryVO) {
		return coordinationOfficeMapper.selectViewDetail(queryVO);
	}
	
	class CoordinationOfficeNoticeExecutor implements Runnable {
			
			private OfficeReqVO reqVO;
			
			private CoordinationOffice record;
			
			public CoordinationOfficeNoticeExecutor(OfficeReqVO reqVO, CoordinationOffice record) {
				this.reqVO = reqVO;
				this.record = record;
			}
	
			@Override
			public void run() {
				try { 
					reqVO.setOfficeId(record.getId());
					CoordinationOfficeSendeeMapper.insertFeedback(reqVO);
					String content = reqVO.getContent();
					if(content.length() > 50) {
						content = content.substring(0, 50) + "...";
					}
					SchoolInfo school = schoolInfoService.queryInitWechatBySchoolId(record.getSchoolId());
					String reqUrl = school.getDomainUrl() + Const.TEACHER_ROLE + MenuConst.COORDINATION_OFFICE + "/myWork/detail/" + reqVO.getOfficeId();
					List<CoordinationOfficeSendee> list = CoordinationOfficeSendeeMapper.selectListByOfficeId(record.getId());
					for(CoordinationOfficeSendee item : list) {
						school = schoolInfoService.refreshInitWechat(school);
						TemplateUtil.coordinationOfficeSend(school,item.getOpenid(), item.getSendee(), content, record.getWorkDate(), record.getTemplateCode(), reqUrl);
					}
		        } catch (Exception e) { 
		            e.printStackTrace(); 
		        }
			}
		}

	@Override
	public List<CoordinationOfficeView> selectListByParam(OfficeQueryVO vo) {
		return coordinationOfficeMapper.selectListByParam(vo);
	}

}
